package meta

import (
	"strings"
)

type CollectorMeta struct {
	fields      []string //分组列列表
	name        string   //索引的名字
	shortName   string   //短名
	accessorKey string   //查询键，但不适用于golang
}

func NewCollectorMeta(fileName string, metaDefine string) *CollectorMeta {
	parts := strings.Split(metaDefine, " ")
	var meta CollectorMeta
	meta.fields = parts[1:]
	meta.buildName()
	meta.buildAccessorKey()
	return &meta
}

func (meta *CollectorMeta) buildName() {
	var name string
	for i, v := range meta.fields {
		if i == 0 {
			name += v
		} else {
			name += strings.ToUpper(v[0:1]) + v[1:]
		}
	}
	meta.shortName = name
	meta.name = name + "Collector"
}

func (meta *CollectorMeta) buildAccessorKey() {
	//TODO 对于golang，数字不能直接与字符串进行拼接
	var key string
	for i, v := range meta.fields {
		if i == 0 {
			key += "\"\" + " + v
		} else {
			key += " + \"_\" + " + v
		}
	}
	meta.accessorKey = key
}

func (meta *CollectorMeta) Fields() []string {
	return meta.fields
}
